package com.yl.auth.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yl.auth.entity.Fangjian;
import com.yl.auth.entity.Louceng;
import com.yl.auth.entity.YlAdmin;
import com.yl.auth.service.FangjianService;
import com.yl.auth.service.LoucengService;
import com.yl.auth.service.YanglaoyuanService;
import com.yl.auth.service.YlAdminService;
import com.yl.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.formula.functions.Count;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ln
 * @since 2023-06-15
 */
@Api(tags = "基础设置-楼层管理")
@RestController
@RequestMapping("/admin/auth/louceng")

public class LoucengController {

    @Resource
    private LoucengService loucengService;
    @Resource
    private FangjianService fangjianService;
    @Resource
    private YlAdminService ylAdminService;

    @ApiOperation(value = "楼层管理全查")
    @GetMapping("/findAllLouceng/{page}/{limit}")
    public Result findAllLouceng(@PathVariable Integer page, @PathVariable Integer limit) {

            Page<Louceng> pagefarm = new Page<>(page,limit);
            IPage<Louceng> page1 = loucengService.getPage(pagefarm);
            for (Louceng louceng : page1.getRecords()){
                List<Fangjian> list = fangjianService.list();
                List<YlAdmin> list2 = ylAdminService.list();
                Integer cc=1;
                for (Fangjian fangjian : list) {
                    if (louceng.getId() == fangjian.getFloorId()) {
                        louceng.setCount(cc++);
                    }
                }
                for (YlAdmin ylAdmin : list2) {
                    if (louceng.getAdminId() == ylAdmin.getId()) {
                        louceng.setAdminname(ylAdmin.getName());
                    }
                }
            }
            return Result.ok(page1);
        }
    @ApiOperation(value = "新增楼层")
    @PostMapping("/saveLouceng")
    public Result saveLouceng(@RequestBody Louceng louceng){
        LocalDateTime now = LocalDateTime.now();
        Integer num = 1;
        louceng.setPid(num);
        louceng.setCreateDate(now);
        loucengService.save(louceng);
        return Result.ok();
    }
    @ApiOperation(value = "删除楼层")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Long id) {
        loucengService.removeById(id);
        return Result.ok();
    }

    @ApiOperation("根据id查询楼层信息")
    @GetMapping("/getById/{id}")
    public Result getById(@PathVariable Long id){
        Louceng louceng = loucengService.selectById(id);
        return Result.ok(louceng);
    }
    @ApiOperation("修改楼层")
    @PutMapping("/update")
    public Result update(@RequestBody Louceng louceng){
        loucengService.updateById(louceng);
        return Result.ok();
    }

}

